package conf

import (
	"gitee.com/manoshi/go-util/util"
)

type Config struct {
	ServerName        string
	Env               string
	HttpPort          int
	LogLevel          string
	LogColorful       bool
	LogResponseLength int
	LogShowSql        bool
	AllowOrigin       []string
	Redis             RedisConf

	Elasticsearch EsConf

	Mysql MysqlConf
}

func BaseConfig(conf Config) {

	ServerName = conf.ServerName
	Env = conf.Env
	HttpPort = conf.HttpPort
	if util.IsEmpty(&conf.LogLevel) {
		LogLevel = "DEBUG"
	} else {
		LogLevel = conf.LogLevel
	}
	LogColorful = conf.LogColorful
	LogResponseLength = conf.LogResponseLength
	LogShowSql = conf.LogShowSql
	AllowOrigin = conf.AllowOrigin
	Es = conf.Elasticsearch
	if Es.StoreDays == 0 {
		Es.StoreDays = 180
	}
	Redis = conf.Redis
	MySql = conf.Mysql
}

// Base conf
var ServerName string
var Env string
var HttpPort int
var AllowOrigin []string

// log conf
var LogLevel = "DEBUG"
var LogColorful = false
var LogResponseLength int
var LogShowSql = true

// Redis conf
type RedisConf struct {
	RedisHost    string
	RedisPort    int
	RedisPwd     string
	RedisDb      int
	ReadTimeout  int //取超时间 单位秒 默认值为3秒
	WriteTimeout int // 写入超时时间 单位秒 默认与读超时时间一致
	PoolSize     int //最大连接数 默认cpu数*10
	MinIdleConns int //最小空闲连接数
}

var Redis = RedisConf{}

// ES conf
type EsConf struct {
	ServerHost         string
	ServerPort         int
	Sniff              bool
	LogType            int //1、只记录用户操作，2、只记录非用户操作，3全记录
	LogIndexNamePrefix string
	UnLogUrls          []string
	CloseClearHisLog   bool   //是否关闭清理历史日志
	ClearHisLogSpec    string //清理历史日志的定时时间配置
	StoreDays          int    //日志存放天数
	CheckDuration      int    //检查间隔时间

}

var Es = EsConf{}

// Mysql conf
type MysqlConf struct {
	Host         string
	Port         int
	User         string
	Password     string
	NameSpace    string
	MaxIdleConns int
	MaxOpenConns int
}

var MySql = MysqlConf{}
